package com.ruoyi.kafka;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @program: ruoyi
 * @description: HongLouService
 * @author: zengchen
 * @create: 2020-11-17 17:06
 **/
@Service
public class HongLouService {

    @Resource(name = "hongLouProducer")
    private Producer<String, String> hongLouProducer;

    public void send(String topic,String key,String value){
        ProducerRecord<String, String> record = new ProducerRecord<>(topic, key, value);
        hongLouProducer.send(record, (metadata, exception) -> {
            System.out.println("callback: " + ToStringBuilder.reflectionToString(metadata));
        });
    }

    public void closeProducer(){
        hongLouProducer.close();
    }
}
